home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1998 April / MACPOWER-1998-04.ISO.7z / MACPOWER-1998-04.ISO / Shareware Paradise / Tabler.091.sit / Tabler 0.9.1 / source code / Tabler.AE.c < prev    next >
Text File  |  1996-06-23  |  6KB  |  199 lines

  1. /*
  2.  *--------------------------------------------------------------
  3.  * Tabler.AE.c
  4.  *--------------------------------------------------------------
  5.  *
  6.  *    prog :    rokkaku
  7.  *
  8.  *--------------------------------------------------------------
  9.  */
  10. #include <AppleEvents.h>
  11. #include <Gestalt.h>
  12. #include <Errors.h>
  13.  
  14. #include "Tabler.h"
  15.  
  16. /* number of required AppleEvents */
  17. enum constAEHandlers {
  18.     kOpenApp    = 0,
  19.     kOpenDoc,
  20.     kPrntDoc,
  21.     kQuitApp,
  22.     kNumOfAE
  23. };
  24.  
  25. /* static functions only used in this source */
  26. static pascal OSErr DoAEOpenApp(const AppleEvent *, AppleEvent *, long);
  27. static pascal OSErr DoAEOpenDoc(const AppleEvent *, AppleEvent *, long);
  28. static pascal OSErr DoAEPrintDoc(const AppleEvent *, AppleEvent *, long);
  29. static pascal OSErr DoAEQuitApp(const AppleEvent *, AppleEvent *, long);
  30. static OSErr GotRequiredParams(const AppleEvent *);
  31.  
  32. /* static global: AppleEvent Hander ProcPtr */
  33. static AEEventHandlerUPP gMyAEUPPs[kNumOfAE] = { nil, nil, nil, nil };
  34.  
  35. /*
  36.  *--------------------------------------------------------------
  37.  * SetUpMyAppleEvent
  38.  *--------------------------------------------------------------
  39.  *    set up Apple Event procedure
  40.  *    if AppleEvent is not supported, do nothing in here
  41.  *--------------------------------------------------------------
  42.  */
  43. Boolean SetUpMyAppleEvent(void)
  44. {
  45.     AEEventID anAEID;
  46.     long    answer;
  47.     OSErr    result;
  48.  
  49.     result = Gestalt(gestaltAppleEventsAttr, &answer);
  50.     if (result != noErr || answer < gestaltAppleEventsPresent) {
  51.         result = errAEHandlerNotFound;
  52.     }
  53.     if (result == noErr) {
  54.         gMyAEUPPs[kOpenApp] = NewAEEventHandlerProc(&DoAEOpenApp);
  55.         gMyAEUPPs[kOpenDoc] = NewAEEventHandlerProc(&DoAEOpenDoc);
  56.         gMyAEUPPs[kPrntDoc] = NewAEEventHandlerProc(&DoAEPrintDoc);
  57.         gMyAEUPPs[kQuitApp] = NewAEEventHandlerProc(&DoAEQuitApp);
  58.     }
  59.     if (result == noErr) {
  60.         result = AEInstallEventHandler(kCoreEventClass, anAEID = kAEOpenApplication,
  61.             gMyAEUPPs[kOpenApp], 0, false);
  62.     }
  63.     if (result == noErr) {
  64.         result = AEInstallEventHandler(kCoreEventClass, anAEID = kAEOpenDocuments,
  65.             gMyAEUPPs[kOpenDoc], 0, false);
  66.     }
  67.     if (result == noErr) {
  68.         result = AEInstallEventHandler(kCoreEventClass, anAEID = kAEPrintDocuments,
  69.             gMyAEUPPs[kPrntDoc], 0, false);
  70.     }
  71.     if (result == noErr) {
  72.         result = AEInstallEventHandler(kCoreEventClass, anAEID = kAEQuitApplication,
  73.             gMyAEUPPs[kQuitApp], 0, false);
  74.     }
  75.     return (true);
  76. }
  77. /*
  78.  *--------------------------------------------------------------
  79.  * RemoveMyAppleEvent
  80.  *--------------------------------------------------------------
  81.  *    delete AppleEvent handlers
  82.  *--------------------------------------------------------------
  83.  */
  84. void RemoveMyAppleEvent(void)
  85. {
  86.     int   i;
  87.  
  88.     for (i = 0; i < kNumOfAE; ++i) {
  89.         if (gMyAEUPPs[i] != nil) {
  90.             DisposeRoutineDescriptor(gMyAEUPPs[i]);
  91.             gMyAEUPPs[i] = nil;
  92.         }
  93.     }
  94. }
  95. /*
  96.  *--------------------------------------------------------------
  97.  * DoAEOpenApp
  98.  *--------------------------------------------------------------
  99.  *    Open Application event: do nothing
  100.  *--------------------------------------------------------------
  101.  */
  102. pascal OSErr DoAEOpenApp(const AppleEvent *theAE, AppleEvent *reply, long refCon)
  103. {
  104.     #pragma unused (theAE, reply, refCon)
  105.     OSErr    result = ConvertScrap();
  106.     gDone = true;
  107.     return (result);
  108. }
  109. /*
  110.  *--------------------------------------------------------------
  111.  * DoAEOpenDoc
  112.  *--------------------------------------------------------------
  113.  *    Open Document event: do open
  114.  *--------------------------------------------------------------
  115.  */
  116. pascal OSErr DoAEOpenDoc(const AppleEvent *theAE, AppleEvent *reply, long refCon)
  117. {
  118.     #pragma unused (reply, refCon)
  119.     AEDescList    fileSpecList = {typeNull, nil};
  120.     FSSpec        thisFileSpec;
  121.     DescType    type;
  122.     AEKeyword    keyword;
  123.     Size        actual;
  124.     long        count = 0;
  125.     long        index;
  126.     OSErr        result;
  127.  
  128.     result = AEGetParamDesc(theAE, keyDirectObject, typeAEList, &fileSpecList);
  129.     if (result == noErr) {
  130.         result = GotRequiredParams(theAE);
  131.     }
  132.     if (result == noErr) {
  133.         result = AECountItems(&fileSpecList, &count);
  134.     }
  135.     if ((result == noErr) && (count > 0)) {
  136.         /* read only one document */
  137.         for (index = 1; index <= 1; index++) {
  138.             result = AEGetNthPtr(&fileSpecList, index, typeFSS, &keyword, &type,
  139.                             (Ptr)&thisFileSpec, sizeof(FSSpec), &actual);
  140.             if (result == noErr) {
  141.                 OpenSourceDoc(&thisFileSpec);
  142.             } else {
  143.                 break;
  144.             }
  145.         }
  146.     }
  147.     AEDisposeDesc(&fileSpecList);
  148.     gDone = true;
  149.     return (result);
  150. }
  151. /*
  152.  *--------------------------------------------------------------
  153.  * DoAEPrintDoc
  154.  *--------------------------------------------------------------
  155.  *    Apple Event Quit procedure
  156.  *--------------------------------------------------------------
  157.  */
  158. pascal OSErr DoAEPrintDoc(const AppleEvent *theAE, AppleEvent *reply, long refCon)
  159. {
  160.     #pragma unused (theAE, reply, refCon)
  161.     return (errAEEventNotHandled);
  162. }
  163. /*
  164.  *--------------------------------------------------------------
  165.  * DoAEQuitApp
  166.  *--------------------------------------------------------------
  167.  *    Apple Event Quit procedure
  168.  *--------------------------------------------------------------
  169.  */
  170. pascal OSErr DoAEQuitApp(const AppleEvent *theAE, AppleEvent *reply, long refCon)
  171. {
  172.     #pragma unused (theAE, reply, refCon)
  173.     gDone = true;
  174.     return (noErr);
  175. }
  176. /*
  177.  *--------------------------------------------------------------
  178.  * GotRequiredParams
  179.  *--------------------------------------------------------------
  180.  *    Apple Event requred parameter parser
  181.  *--------------------------------------------------------------
  182.  */
  183. static OSErr GotRequiredParams(const AppleEvent *theAE)
  184. {
  185.     DescType    returnedType;
  186.     Size        actualSize;
  187.     OSErr        result;
  188.  
  189.     result = AEGetAttributePtr(theAE, keyMissedKeywordAttr, typeWildCard,
  190.                              &returnedType, nil, 0, &actualSize );
  191.  
  192.     if (result == errAEDescNotFound) {
  193.         return (noErr);
  194.     } else if (result == noErr) {
  195.         return (errAEEventNotHandled);
  196.     }
  197.     return (result);
  198. }
  199.